//
//  UIBarButtonItem+Extension.swift
//  weibo
//
//  Created by Ken on 16/3/26.
//  Copyright © 2016年 Ken. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    /// 扩展里面只能添加便利构造函数, 便利构造函数必须调用指定构造函数来创建对象,也可以调用便利构造函数,实际上内容它调用了指定构造函数
    convenience init(imageName: String, target: AnyObject?, action: Selector) {
        //1.自己调用指定构造函数
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        //添加点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //2也可以调用self的便利构造函数,实际上内容它调用了指定构造函数
        self.init(customView: button)
    }
    
    /**
     创建一个带按钮的UIBarButtonItem
     
     - parameter imageName: 图片名称
     
     - returns: UIBarButtonItem
     */
    class func createBarButtonItem(imageName: String) -> UIBarButtonItem {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return UIBarButtonItem(customView: button)
    }
}